package company.meituan_F;

import java.util.*;

public class M3A {

    //深度遍历
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        Map<Integer, Set<Integer>> dict = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int start = sc.nextInt();
            int end = sc.nextInt();
            Set<Integer> set = dict.getOrDefault(start, new HashSet<>());
            set.add(end);
            dict.put(start, set);
        }
        Set<Integer> used = new HashSet<>();
        for (Map.Entry<Integer, Set<Integer>> e : dict.entrySet()) {
            Set<Integer> set11 = new HashSet<>();
            Integer key = e.getKey();
            Queue<Integer> q = new ArrayDeque<>();
            if (!used.contains(key)) {
                q.add(key);
                set11.add(key);
            }
            while (!q.isEmpty()) {
                Integer poll = q.poll();
                if (!used.contains(poll)) {
                    Set<Integer> set = dict.get(poll);
                    for (Integer i : set) {
                        if (!used.contains(i))
                            q.add(i);
                    }
                    q.add(poll);
                    used.add(poll);
                }
            }
            System.out.println(set11);
        }
    }
}
